import {Component, OnInit} from "@angular/core";
import {CommService} from "../comm-service";
import {LocalStorageService} from "../local-storage-service";
import {MyhttpService} from "../myhttp.service";

declare var $: any;

@Component({
  selector: "ksjh-kscc",
  template: `
    <div class="rtop">
      <div class="row">
        <form class="form-horizontal" role="form">
          <div class="col-lg-12">
            <div class="form-group">
              <label class="control-label selectlabel col-lg-5">考试计划</label>
              <div class="col-lg-7">
                <select *ngIf="ksjhArr.length" class="form-control" id="ksjh" name="ksjh" [(ngModel)]="nowksplanId"
                        (change)="ksplanChage()">
                  <option *ngFor="let item of ksjhArr" [value]="item.ksjhbh" [attr.ksxm]="item.ksxmmc">{{item.mc}}
                  </option>
                </select>
              </div>
            </div>
          </div>
          <div class="col-lg-4" style="display: none">
            <div class="form-group">
              <label class="control-label selectlabel col-lg-4">场次</label>
              <div class="col-lg-8">
                <select *ngIf="ksccArr.length" class="form-control" id="kscc" name="kscc" [(ngModel)]="nowccId"
                        (change)="ksccChange()">
                  <option *ngFor="let  item of ksccArr" [value]="item.ccm">
                    {{item.ccmc}}
                  </option>
                </select>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
  `,
  styles: [
    '.rtop{padding: 10px 0px;}',
    '.selectlabel{color: #FCF9F2;}',
    'select::-ms-expand{display: none;}'
  ]
})
export class PublicCompKsjhandcc implements OnInit {
  ksjhArr: any[] = [];
  ksccArr: any[] = [];
  nowksplanId: string = "";
  nowksjhmc: string = "";
  nowccId: string = "";
  nowccmc: string = "";
  nowccflag: string = ""; // 刷新使用的场次唯一标识， 不同的考试计划场次吗可能相同
  nowksxmmc: string = "";

  /**
   * 获取启动中的考试计划
   */
  kspaning(): void {
    const url = "ksyw/ksjhgl/ksjhLb";
    this.itservice.kwPtPost(url, {
      nd: "",
      kszt: "20",
      pageNum: 1,
      pageSize: 9999
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (flag, resp, flag2) => {
        if (flag) {
          if (flag2) {
            const ksjhList_base = resp.ksjhList;
            const ksjhList = this.commservice.sortArr(ksjhList_base, "ksjhbh", "desc");
            this.ksjhArr = ksjhList.map((item) => {
              return {
                ksjhbh: item.ksjhbh,
                mc: item.mc,
                ksxmmc: item.ksxmmc
              }
            });
            const refresh_hksjh = this.localstorage.get("hksjh");
            const refresh_ksxmmc = this.localstorage.get("ksxmmc");
            const refresh_ksjhmc = this.localstorage.get("hksjhmc");
            const refresh_hksjh_isin = this.ksjhArr.some(function (item) {
              return item.ksjhbh == refresh_hksjh;
            });
            if (refresh_hksjh_isin) {
              this.nowksplanId = refresh_hksjh;
              this.nowksxmmc = decodeURIComponent(refresh_ksxmmc);
              this.nowksjhmc = decodeURIComponent(refresh_ksjhmc);
            } else {
              // 没有匹配的，或者压根没有考试计划数据
              this.nowksplanId = this.ksjhArr.length ? this.ksjhArr[0].ksjhbh : "";
              this.nowksxmmc = this.ksjhArr.length ? this.ksjhArr[0].ksxmmc : "";
              this.nowksjhmc = this.ksjhArr.length ? this.ksjhArr[0].mc : "";
            }

            //获取带有优先级的场次列表
            this.ksccing((resp) => {
              if (resp) {
                // 考试计划id肯定存在。且 考试场次接口没报错（考试场次可能有值，可能没值。localstorage 可能有值，可能没值）
                this.localstorage.set("hksjh", this.nowksplanId);
                this.localstorage.set("ksxmmc", encodeURIComponent(this.nowksxmmc));
                this.localstorage.set("hksjhmc", encodeURIComponent(this.nowksjhmc));
                $("#ksjh").selectpicker();
              }
            });
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.kspaning();
        }
      });
    });
  }

  /**
   * 页面操作，更改考试计划， 考试场次势必发生变化,但是需要考试场次在 localstorage 中的值改变后，才能发送通知
   */
  ksplanChage(): void {
    this.ksccing((resp) => {
      this.localstorage.set("hksjh", this.nowksplanId);
      this.nowksxmmc = $("#ksjh  option:selected").attr("ksxm").toString().trim();
      this.nowksjhmc = $("#ksjh  option:selected").text().toString().trim();
      this.localstorage.set("ksxmmc", encodeURIComponent(this.nowksxmmc));
      this.localstorage.set("hksjhmc", encodeURIComponent(this.nowksjhmc));
    });
  }

  /**
   * 根据考试计划编号  获取  考试场次列表
   */
  ksccing(callback: Function): void {
    if (!this.nowksplanId) {
      callback(false);
      return;
    }
    const url = "ksyw/ksjhgl/ksccLb";
    this.itservice.kwPtPost(url, {
      ksjhbh: this.nowksplanId
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (flag, resp, flag2) => {
        if (flag) {
          if (flag2) {
            const ksccList = resp.ksccList;
            this.ksccArr = ksccList.map((item) => {
              return {
                ccm: item.ccm,
                ccmc: item.ccmc,
                kmkssj: item.kmkssj,
                kmjssj: item.kmjssj,
                ccflag: `${this.nowksplanId}${item.ccm}`
              }
            });
            const refresh_kscc = this.localstorage.get("hkscc");
            const refresh_ksccmc = this.localstorage.get("hksccmc");
            const refresh_ksccflag = this.localstorage.get("ccflag");
            const refresh_kscc_isin = this.ksccArr.some(function (item) {
              return item.ccflag == refresh_ksccflag;
            });
            if (refresh_kscc_isin) {
              this.nowccId = refresh_kscc;
              this.nowccflag = refresh_ksccflag;
              this.nowccmc = decodeURIComponent(refresh_ksccmc);
            } else {
              this.nowccId = this.ksccArr.length ? this.ksccArr[0].ccm : "";
              this.nowccflag = this.ksccArr.length ? this.ksccArr[0].ccflag : "";
              this.nowccmc = this.ksccArr.length ? this.ksccArr[0].ccmc : "";
            }
            this.localstorage.set("hkscc", this.nowccId);
            this.localstorage.set("ccflag", this.nowccflag);
            this.localstorage.set("hksccmc", encodeURIComponent(this.nowccmc));
            // 特定考试计划下的考试场次，保存至缓存，供项目页面右上角 考试场次下拉使用
            this.localstorage.set("hcclist", JSON.stringify(this.ksccArr));
            callback(true);
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
            callback(false);
          }
        } else {
          this.ksccing(callback);
        }
      });
    });

  }

  /**
   * 考试场次变化
   */
  ksccChange(): void {
    this.localstorage.set("hkscc", this.nowccId);
    this.localstorage.set("ccflag", `${this.nowksplanId}${this.nowccId}`);
    this.nowccmc = $("#kscc  option:selected").text().toString().trim();
    this.localstorage.set("hksccmc", encodeURIComponent(this.nowccmc));
  }

  constructor(private commservice: CommService,
              private itservice: MyhttpService,
              private localstorage: LocalStorageService) {

  }

  ngOnInit() {
    this.kspaning();
  }
}
